﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        //获取当前节点
        TreeNode currentNode = e.Node;
        Session[currentNode.Value] = currentNode.Value;
    }

    protected void TreeView1_TreeNodeCollapsed(object sender, TreeNodeEventArgs e)
    {
        TreeNode currentNode = e.Node;
        //移除存储的节点
        Session.Remove(currentNode.Value);
    }

    protected void TreeView1_DataBound(object sender, EventArgs e)
    {
        this.TreeView1.TreeNodeExpanded -= new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
        this.TreeView1.TreeNodeCollapsed -= new TreeNodeEventHandler(TreeView1_TreeNodeCollapsed);
        foreach (TreeNode tn in this.TreeView1.Nodes)
        {
            //若循环节点的文本与当前节点的文本匹配,展开该节点；反之折叠
            if (Session[tn.Value] != null)
            {
                tn.Expand();
                foreach (TreeNode tn1 in tn.ChildNodes)
                {
                    if (Session[tn1.Value] != null)
                        tn1.Expand();
                    foreach (TreeNode tn2 in tn1.ChildNodes)
                    {
                        if (Session[tn2.Value] != null)
                            tn2.Expand();
                        foreach (TreeNode tn3 in tn2.ChildNodes)
                        {
                            if (Session[tn3.Value] != null)
                                tn3.Expand();
                        }
                    }
                }
            }
            else
            {
                tn.Collapse();
            }
        }
        this.TreeView1.TreeNodeCollapsed += new TreeNodeEventHandler(TreeView1_TreeNodeCollapsed);
        this.TreeView1.TreeNodeExpanded += new TreeNodeEventHandler(TreeView1_TreeNodeExpanded);
    }

    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        LogoutCache();
        FormsAuthentication.SignOut();
        //Response.Write("<script language=javascript> window.open('Default.htm',target='_top')</script>");
        Response.Write("<script language=javascript> top.location='SignIn.aspx';</script>");
    }

    public void LogoutCache()
    {
        Hashtable h = (Hashtable)Application["online"];
        if (h != null)
        {
            if (h[User.Identity.Name] != null)
                h.Remove(User.Identity.Name);
            Application["online"] = h;
        }
    }  
}
